using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using SmartLink.DTOS;
using SmartLink.DTOS.Permission;
using SmartLink.Application.Services;
using SmartLink.Domain;
using SmartLink.Infrastructure.Cache;
using SmartLink.Application.IServices;
using SmartLink.DTOS.SysBasic;
using SmartLink.DTOS.Base;

namespace SmartLink.WebApi.Controllers
{
    /// <summary>
    /// 权限
    /// </summary>
    public class PermissionController : BaseAPIControl
    {
        private IPermissionAppServices _permissionService;
        public PermissionController(IPermissionAppServices permissionService)
        {
            _permissionService = permissionService;
        }

        #region   menu

        /// <summary>
        /// 获取用户权限菜单json
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [NonAction]
        public Results<TreeNode> GetRightMenuTree()
        {
            CurrentUserInfo curUserInfo = UserInfoCaches.GetUserInfoCache(Token);
            return _permissionService.GetRightMenuTree(curUserInfo.UserId);
        }

        /// <summary>
        /// 获取所有菜单，用于编辑
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [NonAction]
        public Results<TreeNode> GetAllMenuTree()
        {
            return _permissionService.GetAllMenuTree();
        }
        /// <summary>
        ///用户菜单权限查看的目录树
        /// </summary>
        /// <param name="userIDDTO"></param>
        /// <returns></returns>
        [HttpPost]
        [NonAction]
        public Results<TreeNode> GetAllMenuTreeByUserRight([FromBody] UserIDDTO userIDDTO)
        {
            return _permissionService.GetAllMenuTreeByUserRight(userIDDTO.UserId);
        }
        /// <summary>
        ///获取菜单树，角色下有的菜单hasRight：true 
        /// </summary>
        /// <param name="roleIDDTO"></param>
        /// <returns></returns>
        [HttpPost]
        [NonAction]
        public Results<TreeNode> GetAllMenuTreeByRoleId([FromBody] RoleIDDTO roleIDDTO)
        {
            return _permissionService.GetAllMenuTreeByRole(roleIDDTO.RoleId, CurUserInfo.UserId);
        }

        /// <summary>
        /// 获取单个菜单详细
        /// </summary>
        /// <param name="getDTO"></param>
        /// <returns></returns>
        [HttpPost]
        [NonAction]
        public Results<MenuDTO> GetSingleMenu([FromBody] GetIDDTO getDTO)
        {
            return _permissionService.GetSingleMenu(getDTO);
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="inMenuDto"></param>
        /// <returns></returns>
        [HttpPost]
        [NonAction]
        public Results AddMenu([FromBody] InMenuDTO inMenuDto)
        {
            return _permissionService.Add(inMenuDto);
        }
        /// <summary>
        /// 更新菜单
        /// </summary>
        /// <param name="menuDto"></param>
        /// <returns></returns>
        [HttpPost]
        [NonAction]
        public Results UpdateMenu([FromBody] UpDateMenuDto menuDto)
        {
            return _permissionService.Update(menuDto);
        }
        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="deleteDto"></param>
        /// <returns></returns>
        [HttpPost]
        [NonAction]
        public Results DeleteMenu([FromBody] DeleteIDDto deleteDto)
        {
            return _permissionService.Delete(deleteDto);
        }

        #endregion


        /// <summary>
        /// 获取用户行为权限组
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpPost]
        [NonAction]
        public Results<IList<ActionPermissionGroupDTO>> GetActionPermissionGroups([FromBody] int userId)
        {
            return _permissionService.GetActionPermissionGroups(userId);
        }

        /// <summary>
        /// 给角色分配菜单权限
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public Results AllotToRole([FromBody]PermissionAllotToRoleDTO model)
        {
            return _permissionService.AllotToRole(model.RoleID, model.ModuleFunctionModel);
        }

        /// <summary>
        /// 获取角色权限设置界面数据
        /// </summary>
        /// <param name="model">角色ID</param>
        /// <returns>角色权限设置界面数据</returns>
        [HttpPost]
        public Results<IList<PermissionListItemViewDTO>> RolePermissions(IDDTO model)
        {
            return _permissionService.GetRolePermissionsToSetting(model.ID);
        }
    }
}
